(function (win) {
    axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8';

    const service = axios.create({
        baseURL: 'http://localhost:8080',
        timeout: 10000
    });

    // 请求拦截器：如需要 token 可在此统一加
    service.interceptors.request.use(config => {
        // 示例：config.headers['token'] = localStorage.getItem('token')
        return config;
    });

    // 响应拦截器
    service.interceptors.response.use(
        res => {
            console.log('✅ 响应成功：', res);

            // 后端返回 JSON：{ code:0, msg:"NOTLOGIN" }
            if (res.data && res.data.code === 0 && res.data.msg === 'NOTLOGIN') {
                localStorage.removeItem('userInfo');
                window.top.location.replace('/back/page/login/login.html');
                return Promise.reject(res.data);
            }
            return res.data;
        },
        error => {
            console.log('❌ 响应错误：', error);

            // HTTP 状态码 401
            if (error.response && error.response.status === 401) {
                localStorage.removeItem('userInfo');
                window.top.location.replace('/back/page/login/login.html');
                return Promise.reject(error);
            }
            return Promise.reject(error);
        }
    );

    // 挂载到全局
    win.$axios = service;
})(window);